		TITLE	FLUSFINA - Copyright (c) SLR Systems 1994

		INCLUDE	MACROS
		INCLUDE	IO_STRUC
if	fgh_win
		INCLUDE	WINMACS
endif

		PUBLIC	FLUSH_OUTFILE_CLOSE,FLUSH_OUTFILE,FLUSH_FINAL,FF_1,DOSPOSITION_A_PER_OUTFILE,FLUSH_ALMOST_FINAL
		PUBLIC	FLUSH_OUTFILE_2


		.DATA

		EXTERNDEF	_EXE_DEVICE:DWORD,FINAL_TABLE:DWORD,CURN_OUTFILE_GINDEX:DWORD,FINAL_HIGH_WATER:DWORD
		EXTERNDEF	CURN_OUTFILE_BASE_ADDRESS:DWORD,FINAL_RANGES:DWORD

		EXTERNDEF	OUTFILE_GARRAY:STD_PTR_S


		.CODE	PASS2_TEXT

		EXTERNDEF	_dosposition_a:PROC,_do_doswrite2:proc,_do_doswrite_release:proc


FLUSH_OUTFILE_CLOSE	PROC
		;
		;
		;
		CALL	FLUSH_FINAL
		CALL	FLUSH_OUTFILE_1
		ASSUME	EAX:PTR OUTFILE_STRUCT

		OR	[EAX]._OF_FLAGS,MASK OF_CLOSED+MASK OF_TRUNCATED
		RET

FLUSH_OUTFILE_CLOSE	ENDP


FLUSH_OUTFILE	PROC
		;
		;FLUSH LAST USED OUTPUT FILE
		;
		;RETURNS EAX == OUTFILE_STRUCT
		;	 ECX == MYO_STRUCT
		;
		MOV	EAX,CURN_OUTFILE_GINDEX

		TEST	EAX,EAX
		JZ	L9$

		CALL	FLUSH_ALMOST_FINAL		;WRITE OUT LAST PARTIAL BLOCK, NO CLOSE OR TRUNCATE...

		MOV	EAX,_EXE_DEVICE
		ASSUME	EAX:PTR MYO_STRUCT

		XOR	ECX,ECX

		MOV	[EAX].MYO_BUSY,ECX		;RELEASE DEVICE FOR DOS

FLUSH_OUTFILE_1	LABEL	PROC

		MOV	EAX,CURN_OUTFILE_GINDEX
		CONVERT	EAX,EAX,OUTFILE_GARRAY
		ASSUME	EAX:PTR OUTFILE_STRUCT
		MOV	ECX,FINAL_HIGH_WATER

		ADD	[EAX]._OF_FINAL_HIGH_WATER,ECX

FLUSH_OUTFILE_2	LABEL	PROC

		MOV	ECX,_EXE_DEVICE
		ASSUME	ECX:PTR MYO_STRUCT
		XOR	EDX,EDX

if	fgh_outhreads
		BITT	_HOST_THREADED
		JNZ	L5$
endif
		MOV	CURN_OUTFILE_GINDEX,EDX
		XCHG	EDX,[ECX].MYO_HANDLE
L5$:
		MOV	[EAX]._OF_HANDLE,EDX

		MOV	EDX,[ECX].MYO_PHYS_ADDR

		MOV	[EAX]._OF_PHYS_ADDR,EDX

L9$:
		RET

FLUSH_OUTFILE	ENDP


FLUSH_FINAL	PROC
		;
		;FLUSH LAST BLOCK IF ANY LEFT...
		;
		CALL	FLUSH_ALMOST_FINAL

		MOV	EAX,FINAL_HIGH_WATER
		CALL	DOSPOSITION_A_PER_OUTFILE

		ASSUME	EAX:PTR MYO_STRUCT

FF_1		LABEL	PROC

		OR	[EAX].MYO_SPEC_FLAGS,MASK F_TRUNC_FILE+MASK F_CLOSE_FILE
		XOR	ECX,ECX
		push	EDX
		push	ECX
		push	EAX
		CALL	_do_doswrite2
		add	ESP,12
		YIELD

		RET

FLUSH_FINAL	ENDP


DOSPOSITION_A_PER_OUTFILE	PROC
		;
		;EAX IS ADDRESS, RETURN EAX == _EXE_DEVICE
		;
		MOV	ECX,EAX
		MOV	EAX,_EXE_DEVICE

		ADD	ECX,CURN_OUTFILE_BASE_ADDRESS

		push	ECX
		push	EAX
		call	_dosposition_a
		add	ESP,8
		ret

DOSPOSITION_A_PER_OUTFILE	ENDP


FLUSH_ALMOST_FINAL	PROC
		;
		;
		;
		MOV	EAX,FINAL_RANGES+4

		CMP	EAX,-1
		JZ	L1$

		AND	EAX,PAGE_SIZE-1
		JZ	L1$
		;
		;EAX IS # OF BYTES
		;
		;EAX MUST BECOME TARGET ADDRESS
		;
		PUSH	EAX
		MOV	EAX,FINAL_TABLE+4

		SHL	EAX,PAGE_BITS
		CALL	DOSPOSITION_A_PER_OUTFILE

		MOV	EDX,FINAL_TABLE
		POP	ECX

		push	EDX
		push	ECX
		push	EAX
		call	_do_doswrite_release
		add	ESP,12

		YIELD

L1$:
		RET

FLUSH_ALMOST_FINAL	ENDP


		END

